{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using geocoding and geometries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This sample notebook shows how geocoding and geometries can be created and used. The code below shows data to help deduce the path that the debris of a crashed airplane would take if it went down at different places in the ocean. It should be noted that this methodology is for the purpose of demonstrating GIS functionality and the results should not be treated as authoritative. Furthermore, this demo excludes wind or atmospheric conditions, which are critical for real-world analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.gis import GIS\n",
    "from arcgis.geocoding import geocode\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There have been speculations that the unfortunate disappearance of flight [MH370](https://en.wikipedia.org/wiki/Malaysia_Airlines_Flight_370) is tied to [Diego Garcia](https://en.wikipedia.org/wiki/Diego_Garcia), an atoll in the central Indian Ocean, in addition to other search locations in the southern Indian Ocean, west of Australia. The code below plots these locations on the map:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gis = GIS()\n",
    "map1 = gis.map()\n",
    "map1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set the map's extent by geocoding the location\n",
    "diegogarcia = geocode(\"Diego Garcia\", out_sr=102100)[0]\n",
    "diegogarcia_extent = diegogarcia['extent']\n",
    "diegogarcia_extent[\"spatialReference\"] = {\"wkid\": 102100}\n",
    "map1.extent = diegogarcia_extent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# geocode location of La Reunion island\n",
    "lareunion = geocode(\"La Reunion\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.map.popups import PopupInfo\n",
    "popup_info1 = PopupInfo(title=\"La Reunion Island\", description=\"Debris Found\")\n",
    "popup_info2 = PopupInfo(title=\"Diego Garcia\", description=\"Naval Support Facility Diego Garcia\")\n",
    "popup_info3 = PopupInfo(title=\"Search Location\", description=\"Predicted crash location\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.layers import Service\n",
    "from arcgis.geometry import Point\n",
    "\n",
    "# Annotate the map by plotting Diego Garcia, and two other search locations\n",
    "map1.content.draw(lareunion['location'], popup=popup_info1)\n",
    "map1.content.draw(diegogarcia['location'], popup=popup_info2)\n",
    "\n",
    "point = Point({\"x\": 90.5, \"y\": -43.5, \"spatialReference\": {\"wkid\": 4326}})\n",
    "map1.content.draw(point, popup=popup_info3)\n",
    "\n",
    "# Render a feature layer representing the search area\n",
    "# Source: http://www.amsa.gov.au/media/incidents/images/DIGO_00718_01_14.jpg\n",
    "\n",
    "layer = Service(\"http://services.arcgis.com/WQ9KVmV6xGGMnCiQ/arcgis/rest/services/MH370Search/FeatureServer/1\")\n",
    "map1.content.add(layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "map1.zoom = 4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can search ArcGIS Online for content related to the missing airliner, and bring in layers that we want to use for our analysis and visualization:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=cad4c67f0139406fb9c8d3ee8581fd1d' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/cad4c67f0139406fb9c8d3ee8581fd1d/info/thumbnail/thumbnail.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=cad4c67f0139406fb9c8d3ee8581fd1d' target='_blank'><b>Arc</b>\n",
       "                        </a>\n",
       "                        <br/>Lastknownlocationarc<br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by bflanagan_bureau\n",
       "                        <br/>Last Modified: March 02, 2015\n",
       "                        <br/>0 comments, 19747 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Arc\" type:Feature Layer Collection owner:bflanagan_bureau>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=599726f0009a4dd38c06abf036b663c7' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/599726f0009a4dd38c06abf036b663c7/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=599726f0009a4dd38c06abf036b663c7' target='_blank'><b>mh370debris</b>\n",
       "                        </a>\n",
       "                        <br/>mh370 imagery debris<br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by bflanagan_bureau\n",
       "                        <br/>Last Modified: March 02, 2015\n",
       "                        <br/>0 comments, 4949 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"mh370debris\" type:Feature Layer Collection owner:bflanagan_bureau>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=aab7322704084ae1abe1a3d591a8bf72' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/aab7322704084ae1abe1a3d591a8bf72/info/thumbnail/thumbnail.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=aab7322704084ae1abe1a3d591a8bf72' target='_blank'><b>MH370Search</b>\n",
       "                        </a>\n",
       "                        <br/>MH370Search<br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by bflanagan_bureau\n",
       "                        <br/>Last Modified: March 02, 2015\n",
       "                        <br/>0 comments, 11861 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"MH370Search\" type:Feature Layer Collection owner:bflanagan_bureau>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=801585227b494e1ab470e5cf10b3ec53' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/801585227b494e1ab470e5cf10b3ec53/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=801585227b494e1ab470e5cf10b3ec53' target='_blank'><b>UnderwaterSearch</b>\n",
       "                        </a>\n",
       "                        <br/><br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by bflanagan_bureau\n",
       "                        <br/>Last Modified: March 02, 2015\n",
       "                        <br/>0 comments, 4039 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"UnderwaterSearch\" type:Feature Layer Collection owner:bflanagan_bureau>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=7615f1a27fd2410d9452762f94582ff7' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/7615f1a27fd2410d9452762f94582ff7/info/thumbnail/thumbnail.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=7615f1a27fd2410d9452762f94582ff7' target='_blank'><b>SatelliteLocation</b>\n",
       "                        </a>\n",
       "                        <br/>SatelliteLocation<br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by bflanagan_bureau\n",
       "                        <br/>Last Modified: February 26, 2015\n",
       "                        <br/>0 comments, 6832 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"SatelliteLocation\" type:Feature Layer Collection owner:bflanagan_bureau>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=6867dcf7b4214fcfa78e846f4b666a53' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/6867dcf7b4214fcfa78e846f4b666a53/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=6867dcf7b4214fcfa78e846f4b666a53' target='_blank'><b>ActualFlightRoute</b>\n",
       "                        </a>\n",
       "                        <br/><br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by bflanagan_bureau\n",
       "                        <br/>Last Modified: February 26, 2015\n",
       "                        <br/>0 comments, 6409 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"ActualFlightRoute\" type:Feature Layer Collection owner:bflanagan_bureau>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='https://www.arcgis.com/home/item.html?id=86a1b6e71af74979ba38095543d48f07' target='_blank'>\n",
       "                        <img src='https://www.arcgis.com/sharing/rest//content/items/86a1b6e71af74979ba38095543d48f07/info/thumbnail/thumbnail.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='https://www.arcgis.com/home/item.html?id=86a1b6e71af74979ba38095543d48f07' target='_blank'><b>MH370_Flight_Path</b>\n",
       "                        </a>\n",
       "                        <br/>MH370 Flight Path<br/><img src='https://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\" width=16 height=16>Feature Layer Collection by bflanagan_bureau\n",
       "                        <br/>Last Modified: February 25, 2015\n",
       "                        <br/>0 comments, 37930 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"MH370_Flight_Path\" type:Feature Layer Collection owner:bflanagan_bureau>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "mh370items = gis.content.search(\"owner:bflanagan_bureau tags:mh370\", \"feature service\", max_items=7)\n",
    "for item in mh370items:\n",
    "    display(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's add a few of these layers to the map for analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The order of items might change, change the item number according to the order of your notebook\n",
    "map1.content.add(mh370items[6]) # MH370 Flight Path\n",
    "map1.content.add(mh370items[4]) # SatelliteLocation\n",
    "map1.content.add(mh370items[1]) # mh370debris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "map1.zoom = 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we can see, we now have a map containing all of the information associated with this incident, showing the convenience of geocoding for data collection and research. From here, a user could build a custom geoprocessing tool to analyze where the debris may have ended up, based on the data available."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "map1.legend.enabled = True"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "9.0"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
